<?php
namespace app\api\controller\cjaddress;

use app\admin\model\cjaddress\Address;
use app\common\controller\Api;

class Index extends Api
{
    /**
     * 添加地址
     * @ApiMethod (POST)
     * @ApiParams (name="name", type="string", required=true, description="收件人")
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="area", type="string", required=true, description="地区")
     * @ApiParams (name="address", type="string", required=true, description="详细地址")
     * @ApiParams (name="is_default", type="int", required=true, description="是否默认:1=否,2=是")
     */
    public function addAddress()
    {
        $data = $this->request->post();
        $data['user_id'] = $this->auth->id;

        if ($data['is_default'] == 2) {
            Address::where('user_id', $this->auth->id)->update(['is_default' => 1]);
        }
        if (Address::where('user_id', $this->auth->id)->count()) {
            $data['is_default'] = 2;
        }
        $model = new Address();
        $model->save($data);
        $this->success('添加成功');
    }

    /**
     * 地址修改
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="id")
     * @ApiParams (name="name", type="string", required=true, description="收件人")
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="area", type="string", required=true, description="地区")
     * @ApiParams (name="address", type="string", required=true, description="详细地址")
     * @ApiParams (name="is_default", type="int", required=true, description="是否默认:1=否,2=是")
     */
    public function editAddress()
    {
        $data = $this->request->post();
        $find = Address::where('id', $data['id'])->find();
        if (!$find) {
            $this->error('数据不存在');
        }
        if ($data['is_default'] == 2) {
            Address::where('user_id', $this->auth->id)->update(['is_default' => 1]);
        }
        if (Address::where('user_id', $this->auth->id)->count() == 1) {
            $data['is_default'] = 2;
        }
        $model = new Address();
        $model->isUpdate(true)->save($data);
        $this->success('修改成功');
    }

    /**
     * 地址列表
     * @ApiMethod (GET)
     */
    public function addressList()
    {
        $list = Address::where('user_id', $this->auth->id)->select();
        $this->success('ok', $list);
    }

    /**
     * 地址删除
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="int", required=true, description="id")
     */
    public function delAddress()
    {
        $id = $this->request->post('id');
        Address::where('id', $id)->delete();
        $this->success('删除成功');
    }

    /**
     * 地址详情
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="int", required=true, description="id")
     */
    public function addressDetail()
    {
        $id = $this->request->get('id');
        if (empty($id)) {
            $find = Address::where('user_id', $this->auth->id)->where('is_default', 2)->find();
        } else {
            $find = Address::where('id', $id)->find();
        }
        $this->success('ok', $find);
    }

}